package com.yang.strategy.util;


import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author yangyongsheng
 * @date 2024/9/2 23:10
 * @description 获取手机号归属地和运营商的工具
 */
@Component
public class MobileOperatorUtil {

    private final String url360 = "https://cx.shouji.360.cn/phonearea.php?number=";

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 获取手机号信息
     * @param mobile 手机号前7位即可
     * @return
     */
    public String getMobileInfoBy360(String mobile) {
        String url = url360 + mobile;

        //1.发送请求
        String mobileJson = restTemplate.getForObject(url, String.class);

        //2.解析JSON
        JSONObject jsonObject = JSONObject.parseObject(mobileJson);
        int code = jsonObject.getIntValue("code");
        if (code == 0){
            //3.封装位 省市,运营商 的格式返回
            JSONObject jsonData = jsonObject.getJSONObject("data");
            String province = jsonData.getString("province");
            String city = jsonData.getString("city");
            String sp = jsonData.getString("sp");
            return province + city + "," + sp;
        }

        return null;
    }
}
